/*
 * threadsafetype.h
 *
 *  Created on: Jan 13, 2011
 *      Author: dima
 */

#ifndef THREADSAFETYPE_H_
#define THREADSAFETYPE_H_

#include "prerequisites.h"
#include <pthread.h>

template <class T>
class ThreadSafeType
{
	T mValue;
	pthread_mutex_t mMutex;

public:


	ThreadSafeType()
	{
		pthread_mutex_init(&mMutex, NULL);
	}


	~ThreadSafeType()
	{
		pthread_mutex_destroy(&mMutex);
	}


	T operator=(T other)
	{
		mValue = other;
		return mValue;
	}



	T& operator=(T& other)
	{
		mValue = other;
		return mValue;
	}


	operator bool() const
	{
		return !!mValue;
	}


	void lock()
	{
		pthread_mutex_lock(&mMutex);
	}


	void unlock()
	{
		pthread_mutex_unlock(&mMutex);
	}

};

#endif /* THREADSAFETYPE_H_ */
